#include "Node.h"

//Cleans up the m_cszpWalls it holds
Node::~Node()
{
	for (int i = 0; i < m_kiAmountWalls;++i)
	{
		delete [] m_clppMyObject[i];
		delete [] m_clppMyWalls[i];
	}
	delete [] m_clppMyObject;
	delete [] m_clppMyWalls;
}

void Node::Init(int WorldX,int WorldY)
{
	//Used to tell if I need to rotate wall
	COORD DirE = GetCoord(EAST);
	COORD DirW = GetCoord(WEST);

	//Dynamically generates the wall array
	// setting all the m_cszpWalls to x and the center to ' '
	m_kiAmountWalls = AmountWalls;
	m_kiMaxItems	= MaxItems;
	
	
	
	m_clppMyObject = new BaseActivator*[MaxItems];
	for(int NumItems = 0;NumItems < MaxItems;++NumItems)
	{
		m_clppMyObject[NumItems] = new BaseActivator;
	}


	m_clppMyWalls	= new Brick*[m_kiAmountWalls];
	for(int y = 0;y < AmountWalls;++y)
	{
		m_clppMyWalls[y] = new Brick[m_kiAmountWalls];
		for(int x = 0;x < AmountWalls;++x)
		{
			m_clppMyWalls[y][x].m_bActive = true;		
		}
	}

	COORD POS[4];
	POS[NORTH].X = PosMod/2;POS[NORTH].Y = 0;
	POS[SOUTH].X = PosMod/2;POS[SOUTH].Y = PosMod;
	POS[EAST].X = PosMod;POS[EAST].Y = PosMod/2;
	POS[WEST].X = 0;POS[WEST].Y = PosMod/2;
	
	for(int i = 0;i < NullDirection;++i)
	{
		COORD DIR = GetCoord((Directions)i);
		if(i == EAST || i == WEST)
			m_clppMyWalls[DIR.Y][DIR.X].RotateByDegree(0,0,90);

		if(i == EAST || i == SOUTH)
			m_clppMyWalls[DIR.Y][DIR.X].m_bActive = false;

		if((WorldX > (WORLD_WIDTH - 2)) || (WorldY > (WORLD_HEIGHT - 2)))
			m_clppMyWalls[DIR.Y][DIR.X].m_bActive = true;

		m_clppMyWalls[DIR.Y][DIR.X].m_vPosition = D3DXVECTOR3((float)(WorldX * PosMod + POS[i].X),
												0.0f,
												(float)(WorldY * PosMod + POS[i].Y));
	}

				

	m_bVisited = m_bNorth = m_bSouth = m_bEast = m_bWest = false;
}

//Returns true if any direction is possible
bool Node::CanGoDirection()
{
	if(m_bNorth || m_bSouth || m_bEast || m_bWest)
	{
		return true;
	}
	else return false;
}
//Asks if a specific direction is possible
bool Node::CanGoDirection(Directions Direction)
{
	switch(Direction)
	{
	case NORTH:
		return m_bNorth;
		break;
	case SOUTH:
		return m_bSouth;
		break;
	case EAST:
		return m_bEast;
		break;
	case WEST:
		return m_bWest;
		break;
	}
	return false;
}
//Sets a direction to true
void Node::SetDirection(Directions Direction,bool D)
{
	switch(Direction)
	{
	case NORTH:
		m_bNorth = D;
		break;
	case SOUTH:
		m_bSouth = D;
		break;
	case EAST:
		m_bEast = D;
		break;
	case WEST:
		m_bWest = D;
		break;
	}
}
//Breaks down a wall in whatever direction you tell it
// and sets the current node to visited
void Node::BreakWall(Directions Direction)
{
	SetVisitState(true);
	COORD DIR = GetCoord(Direction);
	if(CheckBrick(DIR.X,DIR.Y))
			GetBrick(DIR.X,DIR.Y)->m_bActive = false;
}
//Places the proper char on the map
// Inits the Object pushes it onto the list and returns
void Node::MakeButton(Directions Direction,int X, int Y)
{
	COORD Coord;
	switch(Direction)
	{
	case NORTH:
		Coord = getNorthCoord();

		break;
	case SOUTH:
		Coord = getNorthCoord();

		break;
	case EAST:
		Coord = getNorthCoord();

		break;
	case WEST:
		Coord = getNorthCoord();

		break;
	}
	//m_vppItems[Direction].Type = ButtonID;
	m_clppMyObject[Direction] = new Button;
	m_clppMyObject[Direction]->m_bActive = true;
	m_clppMyObject[Direction]->m_iMyType = BUTTON_TYPE;
	m_clppMyObject[Direction]->m_vPosition = D3DXVECTOR3((float)(X * PosMod + Coord.X * PosMod / 2),
														(float)(Y * PosMod + Coord.Y * PosMod / 2),
														0);
}
//Places the proper char on the map
// Inits the Object pushes it onto the list and returns
void Node::MakeDoor(Directions Direction,int X, int Y)
{
	COORD Coord;
	switch(Direction)
	{
	case NORTH:
		Coord = getNorthCoord();

		break;
	case SOUTH:
		Coord = getNorthCoord();

		break;
	case EAST:
		Coord = getNorthCoord();

		break;
	case WEST:
		Coord = getNorthCoord();

		break;
	}
	//m_vppItems[Direction].Type = DoorID;
	m_clppMyObject[Direction] = new Door;
	m_clppMyObject[Direction]->m_bActive = true;
	m_clppMyObject[Direction]->m_iMyType = DOOR_TYPE;
	m_clppMyObject[Direction]->m_vPosition = D3DXVECTOR3((float)(X * PosMod + Coord.X * PosMod / 2),
												   (float)(Y * PosMod + Coord.Y * PosMod / 2),
												   0);
}